#include "mytcp.h"

#include <QTcpSocket>
#include <QIODevice>
#include <QDebug>

MyTcp::MyTcp(QObject *par):QObject(par)
{
    mytcp = new QTcpServer (this);
    if(!mytcp->listen(QHostAddress::Any, 54321))
    {
        qDebug() << "失败";
        return;
    }

    connect(mytcp, SIGNAL(newConnection()), this, SLOT(newcon()));
}

void MyTcp::newcon()
{
    next = mytcp->nextPendingConnection();

    connect(next, SIGNAL(readyRead()), this, SLOT(read()));
    connect(next, SIGNAL(disconnected()), this, SLOT(close()));
}

void MyTcp::read()
{
    qDebug() << next->readAll();
}

void MyTcp::close()
{
    read();
    qobject_cast<QTcpSocket *>(sender())->close();
    qDebug() << "关闭";
}
